package com.chf.mods;

import com.chf.entities.entity.ZombiePlusEntity;
import com.chf.entities.renderer.ZombiePlusRenderer;
import com.chf.inits.FirstModBlocks;
import com.chf.inits.Items;
import com.chf.inits.Tabs;
import com.chf.items.FirstEntityType;
import com.mojang.logging.LogUtils;
import net.minecraft.core.registries.Registries;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.RegisterEvent;
import org.slf4j.Logger;

/**
 * 模组入口类
 *
 * @author niooooo
 */
@Mod(FirstMod.MOD_ID)
public class FirstMod {

    /**
     * 唯一标识符
     */
    public static final String MOD_ID = "firstmod";

    private static final Logger LOGGER = LogUtils.getLogger();

    /**
     * Mod 构造函数
     *
     * @param context {@link FMLJavaModLoadingContext}
     */
    public FirstMod(FMLJavaModLoadingContext context) {
        var modEventBus = context.getModEventBus();

        modEventBus.register(this.getClass());

        // 1、添加事件监听器
        modEventBus.addListener(this::CommonSetup);
        modEventBus.addListener(this::ClientSetup);
        modEventBus.addListener(FirstMod::addCreative);
        modEventBus.addListener(FirstMod::onRegisterCreativeTabs);
        modEventBus.addListener(this::registerEntityAttributes);
        modEventBus.addListener(this::registerRenderers);

        // 2、延期注册：将 Deferred Register 注册到 mod 事件总线（先注册物品再注册物品栏）
        FirstModBlocks.BLOCKS.register(modEventBus);
        Items.ITEMS.register(modEventBus);
        Tabs.CREATIVE_MODE_TABS.register(modEventBus);
        FirstEntityType.ENTITIES.register(modEventBus);
    }

    private void CommonSetup(final FMLCommonSetupEvent event) {
    }

    private void ClientSetup(final FMLClientSetupEvent event) {
    }

    /**
     * 添加创造模式物品栏
     *
     * @param event {@link BuildCreativeModeTabContentsEvent}
     */
    private static void addCreative(BuildCreativeModeTabContentsEvent event) {
        event.accept(Items.FIRST_ITEM);
        event.accept(Items.RED_STONE_SWORD);
        event.accept(Items.OBSIDIAN_SWORD);
        event.accept(Items.OBSIDIAN_PICKAXE);
        event.accept(Items.OBSIDIAN_AXE);

        event.accept(Items.FIRST_BLOCK);

        event.accept(Items.ZOMBIE_PLUS_SPAWN_EGG);
    }

    /**
     * 物品栏事件监听（非必须 仅日志输出）
     *
     * @param event {@link RegisterEvent}
     */
    private static void onRegisterCreativeTabs(RegisterEvent event) {
        if (event.getRegistryKey().equals(Registries.CREATIVE_MODE_TAB)) {
            LOGGER.info("注册自定义物品栏: {}", Tabs.ITEMS_TAB.getId());
        }
    }

    /**
     * 注册实体属性
     *
     * @param event {@link EntityAttributeCreationEvent}
     */
    private void registerEntityAttributes(EntityAttributeCreationEvent event) {
        event.put(FirstEntityType.ZOMBIE_PLUS.get(), ZombiePlusEntity.createAttributes().build());
    }

    /**
     * 注册渲染器
     *
     * @param event {@link EntityRenderersEvent.RegisterRenderers}
     */
    private void registerRenderers(EntityRenderersEvent.RegisterRenderers event) {
        event.registerEntityRenderer(FirstEntityType.ZOMBIE_PLUS.get(), ZombiePlusRenderer::new);
    }
}
